//
//  UIAlertController+BCHelper.m
//  BCFoundation
//
//  Created by Basic on 2018/3/12.
//

#import "UIAlertController+BCHelper.h"
#import "UIViewController+HHPage.h"

@interface UIAlertController()
@property (weak, nonatomic) UIAlertController     *hh_alert;
@end

@implementation UIAlertController (BCHelper)

#pragma mark - InterfaceOrientations
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskAll;
}
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}
#endif



#pragma mark - system
static UIAlertController *kZHAlertControllerInstance = nil;


#pragma mark - 显示alert 弹窗
+ (void)bc_showAlertMessage:(NSString *)message {
    [self bc_showAlert:nil message:message actionBlock:NULL cancelButtonTitle:@"确定" otherButtonTitles:nil];
}
+ (void)bc_showAlert:(NSString *)title message:(NSString *)message actionBlock:(void(^)(NSInteger bottonIndex) )actionBlock buttonList:(NSArray<NSString *> * )buttonList {
    if (buttonList.count<=0) {
        return;
    }
    UIViewController *topPage = [UIViewController bc_topPage];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [[self class] bc_dismissAlert];
    kZHAlertControllerInstance = alertController;
    NSInteger btnIndex = 0;//按钮索引
    //添加取消按钮
    NSString *cancelStr = buttonList.firstObject;
    if (cancelStr.length>0) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelStr style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex);
            }
        }];
        [alertController addAction:cancelAction];
        btnIndex++;
    }
    //添加其他按钮
    for (NSInteger i=1; i<buttonList.count; i++) {
        NSString *actionStr = buttonList[i];
        UIAlertAction *otherActions = [UIAlertAction actionWithTitle:actionStr style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex);
            }
        }];
        [alertController addAction:otherActions];
        btnIndex++;
    }
    [topPage presentViewController:alertController animated:YES completion:nil];
}

+ (void)bc_showAlert:(NSString *)title message:(NSString *)message actionBlock:(void(^)(NSInteger bottonIndex) )actionBlock cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitles, ... {
    UIViewController *topPage = [UIViewController bc_topPage];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [[self class] bc_dismissAlert];
    kZHAlertControllerInstance = alertController;
    NSInteger btnIndex = 0;//按钮索引
    //添加取消按钮
    if (cancelBtnTitle.length>0) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(0);
            }
        }];
        [alertController addAction:cancelAction];
        btnIndex++;
    }
    //添加其他按钮
    if (otherBtnTitles.length>0) {
        UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex);
            }
        }];
        [alertController addAction:otherActions];
        btnIndex++;
        va_list args;
        va_start(args, otherBtnTitles);
        if (otherBtnTitles.length) {
            NSString *otherString = nil;
            while ((otherString = va_arg(args, NSString*))) {
                UIAlertAction * otherActions = [UIAlertAction actionWithTitle:otherString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    kZHAlertControllerInstance = nil;
                    if (actionBlock) {
                        actionBlock(btnIndex);
                    }
                }];
                [alertController addAction:otherActions];
                btnIndex++;
            }
        }
        va_end(args);
    }
    [topPage presentViewController:alertController animated:YES completion:nil];
}

+ (void)bcs_showAlert:(NSString *)title message:(NSString *)message actionBlock:(void (^)(NSInteger))actionBlock cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitle {
    UIViewController *topPage = [UIViewController bc_topPage];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [[self class] bc_dismissAlert];
    kZHAlertControllerInstance = alertController;
    NSInteger btnIndex = 0;//按钮索引
    //添加取消按钮
    if (cancelBtnTitle.length>0) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(0);
            }
        }];
        [alertController addAction:cancelAction];
        btnIndex++;
    }
    //添加其他按钮
    if (otherBtnTitle) {
        UIAlertAction * otherActions = [UIAlertAction actionWithTitle:otherBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex);
            }
        }];
        [alertController addAction:otherActions];
//        btnIndex++;
    }
    [topPage presentViewController:alertController animated:YES completion:nil];
}


#pragma mark - 显示输入框alert
+ (void)bcs_showInputAlert:(NSString *_Nullable)title message:(NSString *_Nullable)message inputBlock:(void(^_Nullable)(UITextField *textField) )inputBlock  actionBlock:(void(^_Nullable)(NSInteger bottonIndex,UIAlertController *alertController) )actionBlock cancelButtonTitle:(NSString *_Nullable)cancelBtnTitle otherButtonTitle:(NSString *_Nullable)otherBtnTitle {
    //先关闭其他的alert
    [[self class] bc_dismissAlert];
    UIViewController *topPage = [UIViewController bc_topPage];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    //输入框配置
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        if (inputBlock) {
            inputBlock(textField);
        }
    }];
    kZHAlertControllerInstance = alertController;
    NSInteger btnIndex = 0;//按钮索引
    //添加取消按钮
    if (cancelBtnTitle.length>0) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(0, alertController);
            }
        }];
        [alertController addAction:cancelAction];
        btnIndex++;
    }
    //添加其他按钮
    if (otherBtnTitle.length>0) {
        UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex, alertController);
            }
        }];
        [alertController addAction:otherActions];
        btnIndex++;
    }
    [topPage presentViewController:alertController animated:YES completion:nil];
}
+ (void)bc_showInputAlert:(NSString *)title message:(NSString *)message inputBlock:(void(^)(UITextField *textField) )inputBlock  actionBlock:(void(^)(NSInteger bottonIndex,UIAlertController *alertController) )actionBlock cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitles, ... {
    UIViewController *topPage = [UIViewController bc_topPage];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    //输入框配置
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        if (inputBlock) {
            inputBlock(textField);
        }
    }];
    [[self class] bc_dismissAlert];
    kZHAlertControllerInstance = alertController;
    NSInteger btnIndex = 0;//按钮索引
    //添加取消按钮
    if (cancelBtnTitle.length>0) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(0, alertController);
            }
        }];
        [alertController addAction:cancelAction];
        btnIndex++;
    }
    //添加其他按钮
    if (otherBtnTitles.length>0) {
        UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex, alertController);
            }
        }];
        [alertController addAction:otherActions];
        btnIndex++;
        va_list args;
        va_start(args, otherBtnTitles);
        if (otherBtnTitles.length) {
            NSString *otherString = nil;
            while ((otherString = va_arg(args, NSString*))) {
                UIAlertAction * otherActions = [UIAlertAction actionWithTitle:otherString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    kZHAlertControllerInstance = nil;
                    if (actionBlock) {
                        actionBlock(btnIndex, alertController);
                    }
                }];
                [alertController addAction:otherActions];
                btnIndex++;
            }
        }
        va_end(args);
    }
    [topPage presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - 显示actionSheet
+ (void)bc_showActionSheet:(NSString *)title actionBlock:(void(^)(NSInteger bottonIndex) )actionBlock cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherBtnTitles, ...
{
    UIViewController *topPage = [UIViewController bc_topPage];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [[self class] bc_dismissAlert];
    kZHAlertControllerInstance = alertController;
    NSInteger btnIndex = 0;//按钮索引
    //添加取消按钮
    if (cancelBtnTitle.length>0) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex);
            }
        }];
        [alertController addAction:cancelAction];
        btnIndex++;
    }
    //添加其他按钮
    if (otherBtnTitles.length>0) {
        UIAlertAction *otherActions = [UIAlertAction actionWithTitle:otherBtnTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex);
            }
        }];
        [alertController addAction:otherActions];
        btnIndex++;
        //添加剩余按钮
        va_list args;
        va_start(args, otherBtnTitles);
        if (otherBtnTitles.length) {
            NSString * otherString = nil;
            while ((otherString = va_arg(args, NSString*))) {
                UIAlertAction * otherActions = [UIAlertAction actionWithTitle:otherString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    kZHAlertControllerInstance = nil;
                    if (actionBlock) {
                        actionBlock(btnIndex);
                    }
                }];
                [alertController addAction:otherActions];
                btnIndex++;
            }
        }
        va_end(args);
    }
    [[self class] bc_adaptIPAD:alertController withTopPage:topPage];
    [topPage presentViewController:alertController animated:YES completion:nil];
}


+ (void)bc_showActionSheet:(NSString *)title actionBlock:(void(^)(NSInteger bottonIndex) )actionBlock cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonList:(NSArray<NSString *> *)otherButtonList {
    UIViewController *topPage = [UIViewController bc_topPage];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [[self class] bc_dismissAlert];
    kZHAlertControllerInstance = alertController;
    NSInteger btnIndex = 0;//按钮索引
    //添加按钮
    if (cancelBtnTitle.length>0) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            kZHAlertControllerInstance = nil;
            if (actionBlock) {
                actionBlock(btnIndex);
            }
        }];
        [alertController addAction:cancelAction];
        btnIndex++;
    }
    //添加其他按钮
    if (otherButtonList.count>0) {
        for (NSInteger i=0; i<otherButtonList.count; i++) {
            NSString *actionStr = otherButtonList[i];
            UIAlertAction *otherActions = [UIAlertAction actionWithTitle:actionStr style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                kZHAlertControllerInstance = nil;
                if (actionBlock) {
                    actionBlock(btnIndex);
                }
            }];
            [alertController addAction:otherActions];
            btnIndex++;
        }
    }
    [[self class] bc_adaptIPAD:alertController withTopPage:topPage];
    [topPage presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - 关闭弹窗
+ (void)bc_dismissAlert
{
    if (kZHAlertControllerInstance) {
        [kZHAlertControllerInstance dismissViewControllerAnimated:YES completion:nil];
    }
    kZHAlertControllerInstance = nil;
}


#pragma mark - private
+ (void) bc_adaptIPAD:(UIAlertController *)alertController withTopPage:(UIViewController *)topPage {
    if ((UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad)) {
        UIPopoverPresentationController *popover = alertController.popoverPresentationController;
        if (popover) {
            popover.sourceView = topPage.view;
            popover.sourceRect = topPage.view.bounds;
        }
    }
}


@end
